package com.codechen.scaffold.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codechen.scaffold.admin.domain.entity.SysRole;
import com.codechen.scaffold.admin.domain.entity.SysUser;
import com.codechen.scaffold.admin.domain.entity.SysUserRole;
import com.codechen.scaffold.admin.domain.request.RegisterUserRequest;
import com.codechen.scaffold.admin.mapper.SysUserMapper;
import com.codechen.scaffold.admin.service.IRegisterService;
import com.codechen.scaffold.admin.service.ISysRoleService;
import com.codechen.scaffold.admin.service.ISysUserRoleService;
import com.codechen.scaffold.admin.service.ISysUserService;
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author wuweihong
 * @version 1.0
 * @date 2025/6/15 11:31
 * @Description: Please write your descriptions for this class.
 **/

@Service
public class RegisterServiceImpl  extends ServiceImpl<SysUserMapper, SysUser> implements IRegisterService {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Override
    public Long registerUser(RegisterUserRequest registerUserRequest) {
        SysUser user = new SysUser();
        BeanUtils.copyProperties(registerUserRequest, user);
        save(user);
        SysRole role = sysRoleService.getRoleByRoleCode(registerUserRequest.getUserType());
        if (Objects.nonNull(role)) {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(user.getUserId());
            userRole.setRoleId(role.getId());
            sysUserRoleService.save(userRole);
        }
        return user.getId();
    }

}
